﻿using System;
using System.Collections.Generic;
using System.Text;
using ProtocolVN.Framework.Core;
using System.Data.Common;
using System.Data;

namespace ProtocolVN.Framework.Win
{
    public class DAFWObjRelData
    {
        public static DAFWObjRelData I = new DAFWObjRelData();

        private DataTypeBuilder DefineDataTypeBuilder()
        {
            throw new NotImplementedException();
        }

        public bool DeleteDetail(PLTransaction plTrans, DOFWObjRelData Data,long ky_id)
        {
            if (Data == null) return true;            
            
            DatabaseFB db = plTrans.db;
            DbTransaction dbTrans = plTrans.dbTrans;
            try
            {                
                if (!KhoTonKhoHelp.Check_PLQ(plTrans,ky_id, 
                    Data.DES_ID, Data.DES_TYPE))
                {
                    string sql = "delete from kho_ton_kho_" + 
                          ky_id +
                            " where des_id=@des_id and des_type=@des_type and hh_id=@hh_id and tinh_trang_hh=@tinh_trang_hh";
                    DbCommand cmd_Delete = db.GetSQLStringCommand(sql);
                    db.AddInParameter(cmd_Delete, "@des_id", DbType.Int64, Data.DES_ID);
                    db.AddInParameter(cmd_Delete, "@des_type", DbType.Int64, Data.DES_TYPE);
                    db.AddInParameter(cmd_Delete, "@hh_id", DbType.Int64, Data.HH_ID);
                    db.AddInParameter(cmd_Delete, "@tinh_trang_hh", DbType.Int64, Data.TINH_TRANG_HH);

                    db.ExecuteNonQuery(cmd_Delete, dbTrans);
                    //Cập nhật tồn kho từ kỳ hiện tại trở về sau
                    KhoTonKhoHelp.CapNhatNhapXuatChuyen(plTrans, Data, 3,ky_id);
                }
            }
            catch
            {
                return false;
            }
            return true;
        }        

        public bool Delete(PLTransaction plTrans, long ky_id, long des_id, long des_type)
        {            
            DatabaseFB db = plTrans.db;
            DbTransaction dbTrans = plTrans.dbTrans;
            try
            {                
                if (!KhoTonKhoHelp.Check_PLQ(plTrans,ky_id, des_id, des_type))
                {
                    string sql = "delete from kho_ton_kho_" + 
                        ky_id +
                            " where des_id=@des_id and des_type=@des_type";
                    DbCommand cmd_Delete = db.GetSQLStringCommand(sql);
                    db.AddInParameter(cmd_Delete, "@des_id", DbType.Int64, des_id);
                    db.AddInParameter(cmd_Delete, "@des_type", DbType.Int64, des_type);

                    db.ExecuteNonQuery(cmd_Delete, dbTrans);                    
                }
            }
            catch
            {                
                return false;
            }
            return true;
        }        

        public bool Update(PLTransaction plTrans, DOFWObjRelData Data, bool IsUpdate,long ky_id)
        {
            if (Data == null) return true;
            DatabaseFB db = plTrans.db;
            DbTransaction dbTrans = plTrans.dbTrans;
            try
            {                
                if (IsUpdate)
                {
                    string sql = "update kho_ton_kho_" +
                        ky_id +
                        " set src_id=@src_id" +
                        ",src_type=@src_type" +
                        ",so_lg=@so_luong" +
                        ",tong_tr_lg=@tong_trong_luong" +
                        ",nguoi_cap_nhat=@nguoi_cap_nhat" +
                        ",ngay_cap_nhat=@ngay_cap_nhat" +
                        ",nguoi_phat_sinh=@nguoi_phat_sinh" +
                        ",ngay_phat_sinh=@ngay_phat_sinh" +
                        ",kho_id1=@kho_id1" +
                        ",kho_id2=@kho_id2" +
                        ",don_vi_tinh=@don_vi_tinh" +
                        ",gia=@gia" +
                        ",tinh_trang_hh=@tinh_trang_hh" +
                        " where des_id=@des_id" +
                        " and des_type=@des_type" +
                        " and hh_id=@hh_id" +
                        " and tinh_trang_hh=@tinh_trang_hh";

                    DbCommand cmd_Update = db.GetSQLStringCommand(sql);
                    db.AddInParameter(cmd_Update, "@src_id", DbType.Int64, Data.SRC_ID);
                    db.AddInParameter(cmd_Update, "@src_type", DbType.Int32, Data.SRC_TYPE);
                    db.AddInParameter(cmd_Update, "@des_id", DbType.Int64, Data.DES_ID);
                    db.AddInParameter(cmd_Update, "@des_type", DbType.Int32, Data.DES_TYPE);
                    db.AddInParameter(cmd_Update, "@hh_id", DbType.Int64, Data.HH_ID);
                    db.AddInParameter(cmd_Update, "@so_luong", DbType.Decimal, Data.SO_LG);
                    db.AddInParameter(cmd_Update, "@tong_trong_luong", DbType.Decimal, Data.TONG_TR_LG);
                    db.AddInParameter(cmd_Update, "@nguoi_cap_nhat", DbType.Int64, Data.NGUOI_CAP_NHAT);
                    db.AddInParameter(cmd_Update, "@ngay_cap_nhat", DbType.DateTime, Data.NGAY_CAP_NHAT);
                    db.AddInParameter(cmd_Update, "@nguoi_phat_sinh", DbType.Int64, Data.NGUOI_PHAT_SINH);
                    db.AddInParameter(cmd_Update, "@ngay_phat_sinh", DbType.DateTime, Data.NGAY_PHAT_SINH);
                    db.AddInParameter(cmd_Update, "@kho_id1", DbType.Int64, Data.KHO_ID1);
                    db.AddInParameter(cmd_Update, "@kho_id2", DbType.Int64, Data.KHO_ID2);
                    db.AddInParameter(cmd_Update, "@don_vi_tinh", DbType.Int64, Data.DON_VI_TINH);
                    db.AddInParameter(cmd_Update, "@gia", DbType.Decimal, Data.GIA);
                    db.AddInParameter(cmd_Update, "@tinh_trang_hh", DbType.Int64, Data.TINH_TRANG_HH);

                    db.ExecuteNonQuery(cmd_Update, plTrans.dbTrans);
                }
                else
                {                    
                    string sql = "insert into kho_ton_kho_" + 
                        ky_id +
                        " values(@id_tem, @src_id, @src_type, @des_id, @des_type, @hh_id, @so_luong, " +
                        "@tong_trong_luong, @nguoi_cap_nhat, @ngay_cap_nhat, @nguoi_phat_sinh, " + 
                        "@ngay_phat_sinh, @inout1, @kho_id1, @inout2, @kho_id2, @don_vi_tinh, " +
                        "@gia, @tinh_trang_hh)";
                    DbCommand cmd_Insert = db.GetSQLStringCommand(sql);
                    if (Data.ID_TEM <= 0)
                        Data.ID_TEM = db.GetID(HelpGen.G_FW_ID, dbTrans);
                    db.AddInParameter(cmd_Insert, "@id_tem", DbType.Int64, Data.ID_TEM);
                    db.AddInParameter(cmd_Insert, "@src_id", DbType.Int64, Data.SRC_ID);
                    db.AddInParameter(cmd_Insert, "@src_type", DbType.Int32, Data.SRC_TYPE);
                    db.AddInParameter(cmd_Insert, "@des_id", DbType.Int64, Data.DES_ID);
                    db.AddInParameter(cmd_Insert, "@des_type", DbType.Int32, Data.DES_TYPE);
                    db.AddInParameter(cmd_Insert, "@hh_id", DbType.Int64, Data.HH_ID);
                    db.AddInParameter(cmd_Insert, "@so_luong", DbType.Decimal, Data.SO_LG);
                    db.AddInParameter(cmd_Insert, "@tong_trong_luong", DbType.Decimal, Data.TONG_TR_LG);
                    db.AddInParameter(cmd_Insert, "@nguoi_cap_nhat", DbType.Int64, Data.NGUOI_CAP_NHAT);
                    db.AddInParameter(cmd_Insert, "@ngay_cap_nhat", DbType.DateTime, Data.NGAY_CAP_NHAT);
                    db.AddInParameter(cmd_Insert, "@nguoi_phat_sinh", DbType.Int64, Data.NGUOI_PHAT_SINH);
                    db.AddInParameter(cmd_Insert, "@ngay_phat_sinh", DbType.DateTime, Data.NGAY_PHAT_SINH);
                    db.AddInParameter(cmd_Insert, "@inout1", DbType.String, Data.INOUT1);
                    db.AddInParameter(cmd_Insert, "@kho_id1", DbType.Int64, Data.KHO_ID1);
                    db.AddInParameter(cmd_Insert, "@inout2", DbType.String, Data.INOUT2);
                    db.AddInParameter(cmd_Insert, "@kho_id2", DbType.Int64, Data.KHO_ID2);
                    db.AddInParameter(cmd_Insert, "@don_vi_tinh", DbType.Int64, Data.DON_VI_TINH);
                    db.AddInParameter(cmd_Insert, "@gia", DbType.Decimal, Data.GIA);
                    db.AddInParameter(cmd_Insert, "@tinh_trang_hh", DbType.Int64, Data.TINH_TRANG_HH); 
                   
                    db.ExecuteNonQuery(cmd_Insert, dbTrans);
                }
            }
            catch (Exception ex)
            {
                PLException.AddException(ex);
                return false;
            }
            return true;
        }        
    }
}
